﻿//Copyright © Omega Point 2008
//All rights reserved.
//This Software is Licensed under the New BSD License
//for a full copy of this license see http://www.codeplex.com/networkmessenger/license
//Redistributions of source code must retain the above copyright notice.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace NetworkMessenger
{
    public class Message
    {
        private Message()
        {

        }

        private string mTo;
        private string mFrom;
        private string mBody;

        //For Chat
        private bool isChat;
        private Guid _chatId;

        /// <summary>
        /// Creates a New Message based on the Parametres
        /// </summary>
        /// <param name="to">Who the Message is To</param>
        /// <param name="from">Who the Message is From</param>
        /// <param name="message">The Main Body of the Message</param>
        /// <returns>New Message</returns>
        public static Message Create(string to, string from, string message)
        {
            Message tempMessage = new Message();

            tempMessage.mTo = to;
            tempMessage.mFrom = from;
            tempMessage.mBody = message;

            tempMessage.isChat = false;

            return tempMessage;
        }

        /// <summary>
        /// Creates a New Chat Message based on the Parametres
        /// </summary>
        /// <param name="to">Who the Message is To</param>
        /// <param name="from">Who the Message is From</param>
        /// <param name="message">The Main Body of the Message</param>
        /// <param name="chatId">The GUID of the Chat</param>
        /// <returns>New Message</returns>
        public static Message Create(Guid chatId, string to, string from, string message)
        {
            Message tempMessage = new Message();

            tempMessage.mTo = to;
            tempMessage.mFrom = from;
            tempMessage.mBody = message;

            tempMessage._chatId = chatId;
            tempMessage.isChat = true;

            return tempMessage;
        }

        /// <summary>
        /// Converts a XML encoded Stream into a Message
        /// </summary>
        /// <param name="stream">Stream to Convert</param>
        /// <returns>Message based on Stream</returns>
        public static Message FromStream(Stream stream)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(stream);

                XmlNode messageNode = doc.SelectSingleNode("message");

                Message tempMessage = new Message();

                //Check if Message Supports Versioning
                if (messageNode.Attributes["version"] != null)
                {
                    //To Support Chat
                    if (messageNode.Attributes["version"].InnerText == "1.1" && messageNode.Attributes["type"].InnerText == "ChatMessage")
                    {
                        tempMessage.isChat = true;
                        tempMessage._chatId = new Guid(messageNode.Attributes["chatid"].InnerText);
                    }
                }

                tempMessage.mTo = messageNode.SelectSingleNode("to").InnerText;
                tempMessage.mFrom = messageNode.SelectSingleNode("from").InnerText;
                tempMessage.mBody = messageNode.SelectSingleNode("body").InnerText;

                stream.Dispose();

                return tempMessage;
            }
            catch (Exception)
            {
                throw new InvalidDataException();
            }

        }

        /// <summary>
        /// Converts the current message to an XML Encoded string
        /// </summary>
        /// <returns>Message as XML</returns>
        public string ToXml()
        {
            StringBuilder xmlBuilder = new StringBuilder();
            XmlWriter writter = XmlWriter.Create(xmlBuilder);

            writter.WriteStartDocument();

            writter.WriteStartElement("message");

            //To Support Chat
            writter.WriteAttributeString("version", "1.1");
            if (isChat)
            {
                writter.WriteAttributeString("type", "ChatMessage");
                writter.WriteAttributeString("chatid", _chatId.ToString());
            }
            else
            {
                writter.WriteAttributeString("type", "InstantMessage");
            }

            writter.WriteElementString("to", mTo);
            writter.WriteElementString("from", mFrom);
            writter.WriteElementString("body", mBody);
            writter.WriteEndElement();

            writter.WriteEndDocument();

            writter.Flush();
            writter.Close();

            return xmlBuilder.ToString();
        }

        /// <summary>
        /// Gets who the message is to
        /// </summary>
        public string To
        { get { return mTo; } }

        /// <summary>
        /// Gets who the message is from
        /// </summary>
        public string From
        { get { return mFrom; } }

        /// <summary>
        /// Gets the main body of the Message
        /// </summary>
        public string MessageBody
        { get { return mBody; } }

        public bool IsChat
        { get { return isChat; } }

        public Guid ChatID
        { get { return _chatId; } }
    }
}
